// Created by carolsail

import { ENUM_AUDIO_CLIP, ENUM_GAME_MODE, ENUM_GAME_STATUS } from "../Enum";
import { StaticInstance } from "../StaticInstance";
import { random } from "../Utils";
import AudioManager from "../manager/AudioManager";
import DataManager from "../manager/DataManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class Dice extends cc.Component {
    @property([cc.SpriteFrame])
    spriteFrames: cc.SpriteFrame[] = []
    @property
    type: number = 0

    anim: cc.Animation = null

    onLoad() {
        this.node.on('touchstart', this.onTouch, this)
        this.anim = this.node.getComponent(cc.Animation)
        this.anim.on('finished', this.onFinished, this)
    }

    protected onDestroy(): void {
        this.node.off('touchstart', this.onTouch, this)
        this.anim.off('finished', this.onFinished, this)
    }

    onTouch() {
        if (DataManager.instance.status == ENUM_GAME_STATUS.UNRUNING) return
        if (DataManager.instance.isAi[DataManager.instance.round]) return
        if (this.anim.getAnimationState('dice').isPlaying) return
        if (!this.node.getChildByName('arraw').active) return
        this.node.getChildByName('arraw').active = false
        if (DataManager.instance.mode == ENUM_GAME_MODE.SINGLE) {
            DataManager.instance.diceIndex = random(0, 5)
            this.play()
        } else {
            const params = {
                type: 'msg_dice_play',
                room: DataManager.instance.room,
                content: {
                    diceIndex: random(0, 5)
                    // diceIndex: 5
                }
            }
            DataManager.instance.wsClient.callApi('GameSync', params)
        }
    }

    onFinished() {
        this.node.getComponent(cc.Sprite).spriteFrame = this.spriteFrames[DataManager.instance.diceIndex]
        const step = DataManager.instance.diceIndex + 1
        StaticInstance.gameManager.onDiceStep(step)
    }

    play() {
        AudioManager.instance.playSound(ENUM_AUDIO_CLIP.DICE)
        this.anim.play('dice')
    }

    refresh() {
        if (DataManager.instance.status == ENUM_GAME_STATUS.UNRUNING) return
        if (DataManager.instance.mode == ENUM_GAME_MODE.SINGLE) {
            this.node.getChildByName('arraw').active = !DataManager.instance.isAi[DataManager.instance.round]
        } else {
            this.node.getChildByName('arraw').active = false
            if (!DataManager.instance.isAi[DataManager.instance.round] && DataManager.instance.role == DataManager.instance.round) {
                this.node.getChildByName('arraw').active = true
            }
        }
        this.node.getComponent(cc.Sprite).spriteFrame = this.spriteFrames[0]
        if (DataManager.instance.isAi[DataManager.instance.round]) {
            if (DataManager.instance.mode == ENUM_GAME_MODE.SINGLE) {
                DataManager.instance.diceIndex = random(0, 5)
                this.play()
            } else {
                const playerActive = DataManager.instance.room.players.find(player => player.status == 1)
                if (playerActive.openid == DataManager.instance.user.openid) {
                    const params = {
                        type: 'msg_dice_play',
                        room: DataManager.instance.room,
                        content: {
                            diceIndex: random(0, 5)
                        }
                    }
                    DataManager.instance.wsClient.callApi('GameSync', params)
                }
            }
        }
    }
}
